#-------------------------------1.激活函数h(x)
#感知机与神经网络的主要区别就在于激活函数

#1.1 阶跃函数
import numpy as np
import matplotlib.pylab as plt

# def step_function(x):
#     return np.array(x > 0,dtype=np.int64) #将x>0转换为一个数组，并指定数据类型

# x = np.arange(-5.0,5.0,0.1) #max,min,step
# y = step_function(x)
# plt.plot(x,y)
# plt.ylim(-0.1,1.1) #指定Y轴范围
# plt.show()

#1.2 sigmoid函数
def sigmoid(x):
    return 1/(1+np.exp(-x))

# x = np.arange(-5.0,5.0,0.1)
# y = sigmoid(x)
# plt.plot(x,y)
# plt.ylim(-0.1,1.1) #指定Y轴范围
# plt.show()

#----------------------------------------------神经网络的激活函数必须使用非线性函数

#1.3 ReLU函数
def ReLU(x):
    return np.maximum(0,x)

# x = np.arange(-5.0,5.0,0.1)
# y = ReLU(x)
# plt.plot(x,y)
# plt.ylim(-1,6) #指定Y轴范围
# plt.show()


#------------------------------------------2.多维数组的运算----------------------------------#
#2.1 多维数组
# A = np.array([1,2,3,4])
# print(A)
# print(np.ndim(A)) #维数1*4
# print(A.shape)    #元组
# print(A.shape[0])

# B = np.array([[1,2],[3,4],[5,6]])
# print(B)
# print(np.ndim(B)) #维数
# print(B.shape)    #元组3*2
# print(B.shape[0])

#2.2 矩阵的乘法

#------两个二维矩阵
# A = np.array([[1,2,3],[4,5,6]])
# print(A.shape) #2*3
# B = np.array([[1,2],[3,4],[5,6]])
# print(B.shape) #3*2

# print(np.dot(A,B))

# #------二维矩阵与一维数组
# C = np.array([[1,2],[3,4],[5,6]])
# print(C.shape) #3*2
# D = np.array([7,8])
# print(D.shape) #1*2

# print(np.dot(C,D))



#------------------------------------------3.三层神经网络的运算----------------------------------#

#--------------------------输入层到第1层
# X = np.array([1.0,0.5])
# W1 = np.array([[0.1,0.3,0.5],[0.2,0.4,0.6]])
# B1 = np.array([0.1,0.2,0.3])

# print(X.shape)
# print(W1.shape)
# print(B1.shape)

# A1 = np.dot(X,W1)+B1


# #激活函数转换信号 z = ha
# Z1 = sigmoid(A1)

# print(A1)
# print(Z1)

# #--------------------------第1层到第2层

# W2 = np.array([[0.1,0.4],[0.2,0.5],[0.3,0.6]])
# B2 = np.array([0.1,0.2])

# print(Z1.shape)
# print(W2.shape)
# print(B2.shape)

# A2 = np.dot(Z1,W2)+B2


# #激活函数转换信号 z = ha
# Z2 = sigmoid(A2)

# print(A2)
# print(Z2)

# #--------------------------第2层到第3层

# W3 = np.array([[0.1,0.3],[0.2,0.4]])
# B3 = np.array(([0.1,0.2]))
# A3 = np.dot(Z2,W3)+B3
# Y = A3
# print(Y)

#3.1神经网络前向处理的实现
def init_network():
    network = {}
    network['W1'] = np.array([[0.1,0.3,0.5],[0.2,0.4,0.6]])
    network['b1'] = np.array([0.1,0.2,0.3])
    network['W2'] = np.array([[0.1,0.4],[0.2,0.5],[0.3,0.6]])
    network['b2'] = np.array([0.1,0.2])
    network['W3'] = np.array([[0.1,0.3],[0.2,0.4]])
    network['b3'] = np.array(([0.1,0.2]))
    return network

def forward(network,x):
    W1,W2,W3 = network['W1'],network['W2'],network['W3']
    b1,b2,b3 = network['b1'],network['b2'],network['b3']

    a1 = np.dot(x,W1)+b1
    z1 = sigmoid(a1)
    a2 = np.dot(z1,W2)+b2
    z2 = sigmoid(a2)
    a3 = np.dot(z2,W3)+b3

    y  = a3
    return y

network = init_network()
x = np.array([1.0,0.5])
y = forward(network,x)
print(y)


#3.2输出层的设计
#神经网络用于回归和分类问题——对应恒等函数和softmax函数

#softmax函数一般实现————考虑溢出问题
# def softmax(a):
#     exp_a = np.exp(a)
#     sum_exp_a = np.sum(exp_a)
#     y = exp_a / sum_exp_a
#     return y
def softmax(a):
    c = np.max(a)
    exp_a = np.exp(a-c) #溢出对策
    sum_exp_a = np.sum(exp_a)
    y = exp_a / sum_exp_a
    return y

a = np.array([0.3,2.9,4.0])
y = softmax(a)
print(y)


